home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog5.arj / INHERIT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  1.5 KB  |  79 lines

  1. { inherit.pas -- Demonstrate object inheritance }
  2.  
  3. program Inherit;
  4.  
  5. uses WinCrt, Strings;
  6.  
  7. type
  8.  
  9.   PAncestor = ^TAncestor;
  10.   TAncestor = object
  11.     Name: PChar;
  12.     constructor Init(P: PChar);
  13.     destructor Done;
  14.     procedure ChangeName(P: PChar);
  15.     procedure ShowName;
  16.   end;
  17.  
  18.   PDescendant = ^TDescendant;
  19.   TDescendant = object(TAncestor)
  20.     procedure ChangeName(P: PChar);
  21.   end;
  22.  
  23. { TAncestor }
  24.  
  25. constructor TAncestor.Init(P: PChar);
  26. begin
  27.   Name := StrNew(P)
  28. end;
  29.  
  30. destructor TAncestor.Done;
  31. begin
  32.   StrDispose(Name)
  33. end;
  34.  
  35. procedure TAncestor.ChangeName(P: PChar);
  36. begin
  37.   StrDispose(Name);
  38.   Name := StrNew(P)
  39. end;
  40.  
  41. procedure TAncestor.ShowName;
  42. begin
  43.   WriteBuf(Name, StrLen(Name));
  44.   WriteChar(#13);
  45.   WriteChar(#10)
  46. end;
  47.  
  48. { TDescendant }
  49.  
  50. procedure TDescendant.ChangeName(P: PChar);
  51. begin
  52.   TAncestor.ChangeName(P);
  53.   if Name <> nil then StrUpper(Name)
  54. end;
  55.  
  56. var
  57.  
  58.   Parent: PAncestor;
  59.   Child: PDescendant;
  60.  
  61. begin
  62.   Parent := New(PAncestor, Init('String in Parent'));
  63.   Child := New(PDescendant, Init('String in Child'));
  64.   Parent^.ShowName;
  65.   Child^.ShowName;
  66.   Parent^.ChangeName('New string in Parent');
  67.   Child^.ChangeName('New string in Child');
  68.   Parent^.ShowName;
  69.   Child^.ShowName;
  70.   Dispose(Parent, Done);
  71.   Dispose(Child, Done)
  72. end.
  73.  
  74.  
  75. {--------------------------------------------------------------
  76.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  77.   Revision 1.00    Date: 4/08/1991
  78. ---------------------------------------------------------------}
  79.